/**
 * Created by dukes on 2015/11/4.
 */
class FollowItemRender extends eui.ItemRenderer {
    public constructor() {
        super();
        this.skinName = "pet.FollowRenderSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
    }

    public dataChanged(){
        super.dataChanged();
        if( isNaN(this.data)){
            if( this.data["userName"]){
                this.lab_name.text = LimitStringLen(this.data.userName);
                var followed = Model.attentionIdList.indexOf(this.data.userId) != -1;
                this.attn_mark.visible = Model.allAttentionIdList.indexOf(this.data.userId) != -1;
                this.img_cancel_follow.visible = followed;
                this.img_follow.visible = !followed;
                if( Model.me.platform == PlatformEnum.QQBROWSER)
                    this.img_head.source = RES.getRes("default_head_"+(this.data.userId%5));
            }else if(this.data["attentionName"]){
                this.lab_name.text = LimitStringLen(this.data.attentionName);
                var followed = Model.attentionIdList.indexOf(this.data.attentionId) != -1;
                this.attn_mark.visible = Model.allAttentionIdList.indexOf(this.data.attentionId) != -1;
                this.img_cancel_follow.visible = followed;
                this.img_follow.visible = !followed;
                if( Model.me.platform == PlatformEnum.QQBROWSER)
                    this.img_head.source = RES.getRes("default_head_"+(this.data.attentionId%5));
            }
        }else{//searched
            this.lab_name.text = "...";
            var followed = Model.attentionIdList.indexOf(this.data) != -1;
            this.attn_mark.visible = Model.allAttentionIdList.indexOf(this.data) != -1;
            this.img_cancel_follow.visible = followed;
            this.img_follow.visible = !followed;
            if( Model.me.platform == PlatformEnum.QQBROWSER)
                this.img_head.source = RES.getRes("default_head_"+(this.data%5));
        }
         if( Model.me.platform != PlatformEnum.QQBROWSER){
            if( this.data.userFigureurl && this.data.userFigureurl != ""){
                this.img_head.source = this.data.userFigureurl;
            }else if( this.data.attentionFigureurl &&  this.data.attentionFigureurl != ""){
                this.img_head.source = this.data.attentionFigureurl;
            }else{
                this.img_head.source = RES.getRes("default_head_0");
            }
        }

    }

    private onFollow(evt:egret.TouchEvent){
        if(this.data["attentionId"]){
            window["__attention"](this.data.attentionId);
        }else if( this.data["userId"]){
            window["__attention"](this.data.userId);
        }

        DateScene.getInstance().selectedItem = this;
    }
    private onCancelFollow(evt:egret.TouchEvent){
        if(this.data["attentionId"]){
            window["__unattention"](this.data.attentionId);
        }else if( this.data["userId"]){
            window["__unattention"](this.data.userId);
        }

        DateScene.getInstance().selectedItem = this;
    }
    private onAdded(evt:egret.Event){
        this.img_follow.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onFollow, this);
        this.img_cancel_follow.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancelFollow, this);
    }

    private onRemoved(evt:egret.Event){
        this.img_follow.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onFollow, this);
        this.img_cancel_follow.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancelFollow, this);
    }

    private img_follow:eui.Image;
    private img_cancel_follow:eui.Image;
    private img_head:eui.Image;
    private lab_name:eui.Label;
    private attn_mark:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name,instance);
        if( name == "img_follow"){
            this.img_follow = instance;
        }else if( name == "img_cancel_follow"){
            this.img_cancel_follow = instance;
        }else if( name == "img_head"){
            this.img_head = instance;
        }else if( name == "lab_name"){
            this.lab_name = instance;
        }else if( name == "attn_mark"){
            this.attn_mark = instance;
        }
    }
}